package com.cloud.point.concurrent.general;

import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;

/**
 * java线程六种状态
 *
 * @author 24373
 * @version 1.0
 * @date 2024-06-13 18:36
 */
@Slf4j
public class TestThreadState {

    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                log.info("{}", Thread.currentThread().getState());
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {

                }
            }
        });
        t2.start();

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                log.info("t3线程");
            }
        });
        t3.start();

        Thread t4 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (TestThreadState.class) {
                    try {
                        TimeUnit.SECONDS.sleep(3);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        t4.start();

        Thread t5 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 等待t2线程执行完毕
                    t2.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t5.start();

        Thread t6 = new Thread(new Runnable() {
            @Override
            public void run() {
                // 争夺资源；进入阻塞
                synchronized (TestThreadState.class) {
                    try {
                        TimeUnit.SECONDS.sleep(3);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        t6.start();

        log.info("t1 state: {}", t1.getState());
        log.info("t2 state: {}", t2.getState());
        log.info("t3 state: {}", t3.getState());
        log.info("t4 state: {}", t4.getState());
        log.info("t5 state: {}", t5.getState());
        log.info("t6 state: {}", t6.getState());

    }

}
